package com.yummy.cloud.gateway.discover;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
import org.springframework.stereotype.Component;

import com.yummy.cloud.gateway.loadbalancer.LoadBalancerRepository;

import reactor.core.publisher.Flux;

@Component
public class ConfigurableReactiveDiscoverClient implements ReactiveDiscoveryClient{
	
	@Autowired
	private LoadBalancerRepository loadBalancerRepository;

	@Override
	public String description() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Flux<ServiceInstance> getInstances(String serviceId) {
		return Flux.fromIterable(getInstancesFromDB(serviceId));
	}

	private List<ServiceInstance> getInstancesFromDB(String serviceId) {
		return loadBalancerRepository.getInstances(serviceId);
	}

	@Override
	public Flux<String> getServices() {
		return Flux.fromIterable(getServicesFromDB());
	}

	private List<String> getServicesFromDB() {
		return loadBalancerRepository.getServices();
	}

}
